/**
 * Created by Administrator on 2018-08-05.
 * 批文计划
 */

Array.prototype.unique = function() {
    var a = this.concat();
    for(var i=0; i<this.length;i++){
        for(var j=i+1; j<this.length;j++){
            if(a[i] === a[j])
                a.splice(j, 1);
        }
    }
    return a;
};

define(function (require,exports,module) {
    var sTpl = require("./approvalPlan.html");
    require("../../../lib/fullcalendar/moment.min.js");
    var enter = require("./enter1/enter.js");
    var excelEnter = require("../excelEnter/enter.js");
    require("./approvalPlan.css");
    var VueComponent = Vue.extend({
        template: sTpl,
        components:{
            'enters':enter,
            'excelEnter':excelEnter,
        },
        methods:{
            queryAppointment :function(){
                var  exceptDateS = this.form.startDate?this.form.startDate[0]:"";
                var  exceptDateE = this.form.startDate?this.form.startDate[1]:"";
                var  estimatedTimeS = this.form.endDate?this.form.endDate[0]:"";
                var  estimatedTimeE = this.form.endDate?this.form.endDate[1]:"";

                this.urls.url=this.urls.initurl+"&importOrExport=进港"+"&flightInfoId="+this.form.flightInfo.flightInfoId+
                    "&exceptDateS="+exceptDateS+"&exceptDateE="+exceptDateE+"&estimatedTimeS="+estimatedTimeS+"&estimatedTimeE="+estimatedTimeE;
                this.urls.url2=this.urls.initurl+"&importOrExport=出港"+"&flightInfoId="+this.form.flightInfo.flightInfoId+
                    "&exceptDateS="+exceptDateS+"&exceptDateE="+exceptDateE+"&estimatedTimeS="+estimatedTimeS+"&estimatedTimeE="+estimatedTimeE;
                var $this = this;
                axios.get(this.urls.url).then(function (response){
                    $this.importData=response.data;
                });
                axios.get(this.urls.url2).then(function (response){
                    $this.exportData=response.data;
                });
            },
            rowClass:function (row,a) {
                if(row.row.estimatedTime){
                    var today =  new Date(moment().format("YYYY-MM-DD")).getTime();
                    var rowDate =new Date(moment(row.row.estimatedTime).format("YYYY-MM-DD")).getTime() ;
                    if(today==rowDate){
                        return "today-plane";
                    }

                    if(rowDate==today+1000*3600*24*1){
                        return "next-day-plane";
                    }
                    if(rowDate==today+1000*3600*24*2){
                        return "third-day-plane";
                    }
                    if(rowDate==today+1000*3600*24*3){
                        return "fourth-day-plane";
                    }
                    if(rowDate>=today+1000*3600*24*4){
                        return "firth-day-plane";
                    }
                    return "history";
                }
            },



            examineAppointment:function(scope){
                this.$refs.enters.formData=scope.row;
                this.$refs.enters.parentData.showOrEdit='show';
                this.$refs.enters.parentData.dataCallBack=function(data){
                    scope.row=data;
                }
                $("#panel-activer").click();
            },
            openExcel:function () {
                this.flightPlaneExcel = true;
            },
            addPlane:function () {
                this.title="新增批文航班计划"
                this.flightPlaneEdit=true;
                this.enterData.editOrAdd='add';
                var $this= this;
                this.enterData.editCallback=function (val) {
                    $this.flightPlaneEdit=false;
                    $this.queryAppointment();
                }
                this.enterData.openEdit=false;
            },
            editFlightPlane:function(scope){
                this.title="批文航班计划修改"
                this.flightPlaneEdit=true;
                this.enterData.editOrAdd='edit';
                var $this= this;
                this.enterData.openEdit=function (a) {
                    a(JSON.parse(JSON.stringify(scope.row)));
                }
                this.enterData.editCallback=function (val) {
                    $this.flightPlaneEdit=false;
                    $this.queryAppointment();
                }

            },
            editAppointment:function (scope) {
                this.appointmentEdit=true;
                this.appointment =scope.row;
                this.handleSelect(scope.row.flightInfo.flightInfoId);
                this.currentAppointmentScope = scope;
            },
            handleSelect:function (item) {
                this.flightInfo =this.planes.find(function (val) {
                    return val.flightInfoId ==item;
                })
                console.log(item);
            },
            pickExpectDate:function (val) {
                if(val==null){
                    this.appointment.appoDate = "";
                }else{
                    this.appointment.appoDate = moment(val).format("YYYY-MM-DD");;
                }

            },
            pickEstimatedTime:function (val) {
                if(val==null){
                    this.appointment.estimatedTime = "";
                }else{
                    this.appointment.estimatedTime = moment(val).format("YYYY-MM-DD");;
                }

            },
            querySearchPlace:function(queryString, cb) {
                var restaurants = this.citys;
                var results = queryString ? restaurants.filter(function(restaurant){
                    return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
                }) : restaurants;
                // 调用 callback 返回建议列表的数据
                cb(results);
            },
            confirmEditAppointment :function(){
                var $this = this ;
                this.$refs["appointment"].validate(function(valid) {
                    if (valid) {
                        var data = JSON.parse(JSON.stringify($this.appointment));
                        if(!data.agreement||!data.agreement.contractManageId){
                            data.agreement=null;
                        }
                        axios.post(basepath+'/api/appointment/saveAppointmentAndFlight',data)
                            .then(function (response) {
                                $this.appointmentEdit=false;
                                $this.$notify({
                                    title: '修改成功',
                                    message: '修改成功'
                                });
                            });
                        return true;
                    } else {
                        $this.$notify({
                            title: '校验失败',
                            message: '请检查修改字段'
                        });
                    }
                });
            },
            initMessage:function () {
                var now =  moment();
                var date = moment().minute(now.minute()-20);
                for(var i=0;i< 5;i++){
                    var time = date.minute(date.minute()-10).format("YYYY-MM-DD HH:mm:ss");

                    this.sentMessages.read.push({
                        time:time,
                        message:"SPECI EPDL 100029Z 06006mps 9999-SHRA \n"+
                        "FEW016 FEW026CB Q1022 RETSRA NOSIG-"
                    });
                    this.receivedMessage.read.push({
                        time:time,
                        message:" ZCZC TSCXXX 120110  \n"+
                        "FF ZSCNZXZX ZSCNZQZX ZUCKZPZX-"
                    })
                }
                var time =  moment().minute(now.minute()-20).format("YYYY-MM-DD HH:mm:ss");
                for(var i=0;i<4;i++){
                    this.sentMessages.unRead.push({
                        time:time,
                        message:"BMPEC RPGK 100029Z 06006mps 9999-SHRA \n"+
                        "FEW016 FEW026CB Q1022 RETSRA NOSIG-"
                    });
                    this.receivedMessage.unRead.push({
                        time:time,
                        message:" KGKG TBHKKK 120110  \n"+
                        "FF ZSCNZXZX ZSCNZQZX ZUCKZPZX-"
                    })
                }

            },
            receivedUnReadMessage:function () {
                this.messageTitle="已收报文";
                this.tabList=this.sentMessages.unRead;
                this.isSentMessages=false;
                this.isReceivedMessages=true;
                this.messageDialog=true;
            },
            /*未过滤时间条件*/
            checkMessages:function () {
                if(this.checked){
                    this.messageTitle="已发报文";
                    this.tabList=this.sentMessages.read;
                }else{
                    this.messageTitle="已收报文";
                    this.tabList=this.receivedMessage.read;
                }
                this.isSentMessages=false;
                this.isReceivedMessages=false;

                this.messageDialog=true;

            },
            sentUnReadMessage:function () {
                this.messageTitle="已发报文";
                this.tabList=this.sentMessages.unRead;
                this.isSentMessages=true;
                this.isReceivedMessages=false,
                this.messageDialog=true;
            },
            closeNewMessage:function () {
                if(this.isSentMessages){
                    for(var i=0;i<this.sentMessages.unRead.length;i++){
                        this.sentMessages.read.unshift(this.sentMessages.unRead[i])
                    }
                    this.sentMessages.unRead=[];
                };
                if(this.isReceivedMessages){
                    for(var i=0;i<this.receivedMessage.unRead.length;i++){
                        this.receivedMessage.read.unshift(this.receivedMessage.unRead[i])
                    }
                    this.receivedMessage.unRead=[];
                }
                this.messageDialog = false;
            }
        },
        watch:{

        },

        mounted:function() {
            this.queryAppointment();
            this.initMessage();
        }
        ,data:function(){
            var $this = this;
            return {
                isReceivedMessages:false,
                isSentMessages:false,
                tabList:[],
                title:"批文航班计划修改",
                flightPlaneEdit:false,
                flightPlaneExcel :false,
                messageTitle:"",
                messageDialog:false,
                currentAppointmentScope:{},
                excelEnterData:{
                    planeProperty:"批文",
                    cancelCallback:function () {
                        $this.flightPlaneExcel= false;
                    },
                    callback:function () {
                        $this.flightPlaneExcel= false;
                        $this.queryAppointment();
                    }
                },
                diawidth:"750px",
                form: {

                    appoUnit: '',
                    flightInfo:{flightInfoId:""},
                    startDate:'',
                    endDate:[moment("2018-08-16").format("YYYY-MM-DD"),moment(new Date("2018-08-16").getTime() + 3600 * 1000 * 24 * 7).format("YYYY-MM-DD")],
                    unExamine:false,
                    appoCancle:false,
                    examinePass:false,
                    examineCancle:false,
                    appoPhoneNum:'',
                    type:""
                },
                enterData:{
                    editOrAdd:'add',
                    unShowExcel:"true",
                    planeProperty:'批文',
                    onlyTime:true
                },
                date1:'',
                date2:'',
                checked:false,
                enterOpen:true,
                tableData:[],
                importData:[],
                exportData:[],
                units: [],
                urls:{
                    url:basepath+"/api/flightPlane/findInterimFlightPlaneNoPage?planeProperty=批文&importOrExport=进港",
                    url2:basepath+"/api/flightPlane/findInterimFlightPlaneNoPage?planeProperty=批文&importOrExport=出港",
                    initurl:basepath+"/api/flightPlane/findInterimFlightPlaneNoPage?planeProperty=批文&order=estimatedTime",
                },
                formData: [],
                appointment:{flightInfo:{flightInfoId:""}},
                flightInfo:{},
                planes:[],
                pickerOptions2: {
                    shortcuts: [{
                        text: '最近一周',
                        onClick:function(picker) {
                            const end = moment().format("YYYY-MM-DD");
                            const start = moment(new Date().getTime() + 3600 * 1000 * 24 * 7).format("YYYY-MM-DD");
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick:function(picker) {
                            const end = moment().format("YYYY-MM-DD");
                            const start = moment(new Date().getTime() + 3600 * 1000 * 24 * 30).format("YYYY-MM-DD");
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick:function(picker) {
                            const end = moment().format("YYYY-MM-DD");
                            const start = moment(new Date().getTime() + 3600 * 1000 * 24 * 90).format("YYYY-MM-DD");
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                citys:[{value:'北京'},
                    {value:'天津'},
                    {value:'上海'},
                    {value:'徐州'},
                    {value:'常州 '},
                    {value:'江苏'},
                    {value:'南通'},
                    {value:'无锡'},
                    {value:'南京'},
                    {value:'连云港'},
                    {value:'济南'},
                    {value:'临沂'},
                    {value:'潍坊'},
                    {value:'威海'},
                    {value:'烟台'},
                    {value:'青岛'},
                    {value:'济宁'},
                    {value:'黄岩路'},
                    {value:'衢州'},
                    {value:'舟山'},
                    {value:'宁波'},
                    {value:'温州'},
                    {value:'杭州'},
                    {value:'锦州'},
                    {value:'朝阳'},
                    {value:'沈阳'},
                    {value:'大连'},
                    {value:'赤峰'},
                    {value:'内蒙古'},
                    {value:'锡林浩特'},
                    {value:'包头'},
                    {value:'呼和浩特'},
                    {value:'乌兰浩特'},
                    {value:'海拉尔'},
                    {value:'乌海'},
                    {value:'呼伦贝尔'},
                    {value:'通辽'},
                    {value:'南平市'},
                    {value:'厦门'},
                    {value:'泉州'},
                    {value:'晋江'},
                    {value:'福州'},
                    {value:'黄山屯溪'},
                    {value:'安庆'},
                    {value:'合肥'},
                    {value:'阜阳'},
                    {value:'蚌埠'},
                    {value:'嘉峪关'},
                    {value:'酒泉'},
                    {value:'庆阳'},
                    {value:'兰州'},
                    {value:'深圳'},
                    {value:'珠海'},
                    {value:'梅县'},
                    {value:'湛江'},
                    {value:'汕头'},
                    {value:'北海福城'},
                    {value:'广西'},
                    {value:'梧州'},
                    {value:'柳州'},
                    {value:'南宁'},
                    {value:'桂林'},
                    {value:'贵州'},
                    {value:'兴义'},
                    {value:'铜仁'},
                    {value:'黎平'},
                    {value:'龙洞堡'},
                    {value:'荔波'},
                    {value:'海口'},
                    {value:'三亚'},
                    {value:'秦皇岛'},
                    {value:'邯郸'},
                    {value:'邢台'},
                    {value:'石家庄'},
                    {value:'洛阳'},
                    {value:'南阳'},
                    {value:'郑州'},
                    {value:'黑河'},
                    {value:'佳木斯'},
                    {value:'哈尔滨'},
                    {value:'齐齐哈尔'},
                    {value:'牡丹江'},
                    {value:'武汉'},
                    {value:'宜昌'},
                    {value:'襄樊'},
                    {value:'恩施'},
                    {value:'常德'},
                    {value:'张家界'},
                    {value:'长沙'},
                    {value:'怀化'},
                    {value:'延吉'},
                    {value:'长白山'},
                    {value:'吉林'},
                    {value:'长春'},
                    {value:'德镇'},
                    {value:'井冈山'},
                    {value:'九江庐山'},
                    {value:'吉安'},
                    {value:'南昌'},
                    {value:'赣州'},
                    {value:'银川'},
                    {value:'西宁'},
                    {value:'格尔木'},
                    {value:'运城'},
                    {value:'太原'},
                    {value:'大同'},
                    {value:'长治'},
                    {value:'西安'},
                    {value:'安康'},
                    {value:'延安'},
                    {value:'榆林'},
                    {value:'汉中'},
                    {value:'成都'},
                    {value:'康定'},
                    {value:'达县'},
                    {value:'西昌'},
                    {value:'绵阳'},
                    {value:'四川攀枝花'},
                    {value:'九寨沟'},
                    {value:'广汉'},
                    {value:'南充'},
                    {value:'宜宾'},
                    {value:'林芝'},
                    {value:'昌都邦达'},
                    {value:'拉萨贡嘎'},
                    {value:'阿勒泰'},
                    {value:'伊宁'},
                    {value:'喀纳斯'},
                    {value:'喀什'},
                    {value:'库尔勒'},
                    {value:'克拉玛依'},
                    {value:'库车'},
                    {value:'且末'},
                    {value:'富蕴'},
                    {value:'和田'},
                    {value:'哈密'},
                    {value:'伊犁那拉提'},
                    {value:'鄯善'},
                    {value:'塔城'},
                    {value:'乌鲁木齐'},
                    {value:'阿克苏'},
                    {value:'景洪'},
                    {value:'昭通'},
                    {value:'文山普者黑'},
                    {value:'普洱'},
                    {value:'大理'},
                    {value:'丽江'},
                    {value:'云南迪庆香格里拉'},
                    {value:'德宏芒市'},
                    {value:'昆明巫家坝'},
                    {value:'重庆'},
                    {value:'香港'},
                    {value:'澳门'},
                    {value:'台北'},
                    {value:'桃园'},
                    {value:'台中'},
                    {value:'台东'},
                    {value:'台南'}
                ],
                sentMessages:{read:[],unRead:[]},
                receivedMessage:{read:[],unRead:[]}
            }
        },
        created:function (){
            var $this = this;
            axios.get(basepath+"/api/flightInfo/queryFlightInfo?filter=").then(function (response) {
                $this.planes=response.data;
                $this.planesMap={};
                $this.planes.forEach(function(plane){
                    plane.value= plane.flightNum;
                    $this.planesMap[plane.flightNum] = plane;
                })
            })
        },
        activated:function () {
            messages.$emit('change','批文计划');
        }
    });
    module.exports = VueComponent;
})
